﻿using Model;

namespace Dal
{
    public class storeDal
    {
        private readonly MyDbContext db;

        public storeDal(MyDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 门店增加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostAddStore(T_StoreInfo info)
        {
            try
            {
                db.T_StoreInfo.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 门店名称唯一
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int GetSearchName(string name)
        {
            try
            {
                var  list = db.T_StoreInfo.Where(x=>x.Sname == name).Count();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 显示数据查询
        /// </summary>
        /// <param name="name">门店名称</param>
        /// <param name="phone">门店电话</param>
        /// <param name="indexs">总页数</param>
        /// <param name="counts">总条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示几条</param>
        /// <returns></returns>
        public List<T_StoreInfo> GetStoreList(string name,string phone,out int indexs,out int counts,int pageIndex =1 ,int pageSize =2)
        {
            try
            {
                var list = db.T_StoreInfo.Where(x=>x.IsDeleted == true).ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x=>x.Sname.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(phone))
                {
                    list = list.Where(x=>x.Sphone.Contains(phone)).ToList();
                }
                counts = list.Count();
                indexs = (int)Math.Ceiling(counts * 1.0 / pageSize);
                list = list.OrderBy(x=>x.Sid).Skip((pageIndex -1 )/pageSize).Take(pageSize).ToList();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetDeleteInfo(int id)
        {
            try
            {
                var res = db.T_StoreInfo.Find(id);
                res.IsDeleted = false;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 优惠券添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostDiscountAdd(T_Discount info)
        {
            try
            {
                db.T_Discount.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 优惠券名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int SearchName(string name)
        {
            try
            {
                var list = db.T_Discount.Where(x=>x.Dname == name).Count();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}